<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="商店名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商店名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商店类型" prop="storeType">
        <el-select v-model="queryParams.storeType" placeholder="选择商店类型" clearable style="width: 200px">
          <el-option label="食品" :value="1" />
          <el-option label="服装" :value="2" />
          <el-option label="电子产品" :value="3" />
          <el-option label="书籍" :value="4" />
          <el-option label="其他" :value="5" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd">新增商店</el-button>
      </el-col>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="storeList" row-key="id">
      <el-table-column prop="name" label="商店名称" width="200"></el-table-column>
      <el-table-column prop="location" label="商店位置" width="150"></el-table-column>
      <el-table-column prop="contactNumber" label="客服电话" width="150"></el-table-column>
      <el-table-column prop="operatingHours" label="经营时间" width="150"></el-table-column>
      <el-table-column prop="storeType" label="商店类型" width="100">
        <template #default="scope">
          <span>{{ scope.row.storeType === 1 ? "食品" : scope.row.storeType === 2 ? "服装" : scope.row.storeType === 3 ? "电子产品" : scope.row.storeType === 4 ? "书籍" : "其他" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="storeFormat" label="商店格式" width="100">
        <template #default="scope">
          <span>{{ scope.row.storeFormat === 1 ? "固定店铺" : "流动商贩" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isOperating" label="是否在营业" width="100">
        <template #default="scope">
          <span>{{ scope.row.isOperating ? "是" : "否" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="owner" label="商店所有者" width="150"></el-table-column>
      <el-table-column prop="rating" label="商店评分" width="100"></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改商店对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form ref="storeRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商店名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入商店名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商店位置" prop="location">
              <el-input v-model="form.location" placeholder="请输入商店位置" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商店所有" prop="owner">
              <el-input v-model="form.owner" placeholder="请输入商店所有者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客服电话" prop="contactNumber">
              <el-input v-model="form.contactNumber" placeholder="请输入客服电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营时间" prop="operatingHours">
              <el-input v-model="form.operatingHours" placeholder="请输入经营时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商店类型" prop="storeType">
              <el-select v-model="form.storeType" placeholder="选择商店类型">
                <el-option label="食品" :value="1" />
                <el-option label="服装" :value="2" />
                <el-option label="电子产品" :value="3" />
                <el-option label="书籍" :value="4" />
                <el-option label="其他" :value="5" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商店评分" prop="rating">
              <el-input v-model="form.rating" placeholder="请输入商店评分" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商店格式" prop="storeFormat">
              <el-select v-model="form.storeFormat" placeholder="选择商店格式">
                <el-option label="固定店铺" :value="1" />
                <el-option label="流动商贩" :value="2" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否营业" prop="isOperating">
              <el-switch v-model="form.isOperating" active-text="是" inactive-text="否" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="关联设施" prop="storeId">
              <el-select v-model="form.facilityId" placeholder="请选择设施">
                <el-option v-for="facility in factilitiesList" :key="facility.id" :label="facility.name" :value="facility.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="商店描述" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入商店描述" />
            </el-form-item>
          </el-col>
         
         
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="StoreList">
import { ref, reactive } from 'vue';
import { listStores, addStore, updateStore, deleteStore } from "@/api/pages/virtualStore";
import { listFacilities } from "@/api/pages/facility";
const loading = ref(true);
const open = ref(false);
const showSearch = ref(true);
const title = ref("");
const refreshTable = ref(true);
const storeList = ref([]);
const factilitiesList = ref([]);
const form = reactive({
  id: undefined,
  name: "",
  description: "",
  location: "",
  owner: "",
  contactNumber: "",
  operatingHours: "",
  storeType: 1,
  rating: 0.0,
  storeFormat: 1,
  isOperating: true,
  userId: undefined,
  facilityId: undefined
});
const queryParams = reactive({
  name: "",
  storeType: undefined
});
const rules = {
  name: [{ required: true, message: "商店名称不能为空", trigger: "blur" }],
};

// 查询商店列表
const getList = () => {
  loading.value = true;
  listStores(queryParams).then(response => {
    storeList.value = response.data; // 确保 response.data 是数组
    loading.value = false;
  }).catch(error => {
    console.error("获取商店列表失败:", error);
    loading.value = false;
  });
};

// 获取设施列表
const getFacilities = () => {
  listFacilities().then(response => {
    factilitiesList.value = response.data;
  }).catch(error => {
    console.error("获取设施列表失败:", error);
  });
};


// 取消按钮
const cancel = () => {
  open.value = false;
  reset();
};

// 重置表单
const reset = () => {
  Object.assign(form, {
    id: undefined,
    name: "",
    description: "",
    location: "",
    owner: "",
    contactNumber: "",
    operatingHours: "",
    storeType: 1,
    rating: 0.0,
    storeFormat: 1,
    isOperating: true,
    userId: undefined
  });
};

// 搜索按钮操作
const handleQuery = () => {
  getList();
};

// 重置按钮操作
const resetQuery = () => {
  Object.assign(queryParams, {
    name: "",
    storeType: undefined
  });
  getList();
};

// 新增按钮操作
const handleAdd = () => {
  reset();
  open.value = true;
  getFacilities();
  title.value = "新增商店";
};

// 修改按钮操作
const handleUpdate = (row) => {
  Object.assign(form, row);
  open.value = true;
  title.value = "修改商店";
};

// 提交按钮
const submitForm = () => {
  const apiCall = form.id ? updateStore : addStore;
  apiCall(form).then(() => {
    getList();
    open.value = false;
  }).catch(error => {
    console.error("操作失败:", error);
  });
};

// 删除按钮操作
const handleDelete = (row) => {
  deleteStore(row.id).then(() => {
    getList();
  }).catch(error => {
    console.error("删除失败:", error);
  });
};

// 初始化商店列表
getList();
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
.dialog-footer {
  text-align: right;
}
</style>